<?php

namespace app\services;

use app\payment\PayFactory;
use plugin\admin\app\model\BindUser;
use plugin\admin\app\model\User;
use plugin\admin\app\model\UserLevel;
use plugin\admin\app\model\UserProfile;
use plugin\admin\app\model\UserProperty;
use plugin\admin\app\model\UserPropertyLog;
use Webman\Util;

class BindUserService
{
    public static function bindByNameAndPassword($userId, $username, $password)
    {
        $user = User::where('mobile', $username)->first();

        if (!$user) {
            throw new \Exception('不存在的用户');
        }

        if (!\plugin\admin\app\common\Util::passwordVerify($password, $user['password'])) {
            throw new \Exception('密码不正确');

        }

        return self::bind($userId, $user['id']);
    }

    public static function bindList($userId)
    {
        $bindUsers = BindUser::where('user_id', $userId)->get()->toArray();

        $userIds = array_column($bindUsers, 'bind_user_id');

        $userProfiles = UserProfile::select(['user_id', 'nickname', 'avatar'])->whereIn('user_id', $userIds)->get()->toArray();
        $userProfiles = array_combine(
            array_column($userProfiles, 'user_id'),
            array_values($userProfiles)
        );

        $userPropertyList = UserProperty::select(['user_id', 'integral', 'recharge_integral', 'register_integral'])->whereIn('user_id', $userIds)->get()->toArray();
        $userPropertyList = array_combine(
            array_column($userPropertyList, 'user_id'),
            array_values($userPropertyList)
        );

        $users = User::select(['id', 'mobile', 'level'])->whereIn('id', $userIds)->get()->toArray();
        $users = array_combine(
            array_column($users, 'id'),
            array_values($users)
        );

        $levels = UserLevel::list();
        $levels = array_combine(
            array_column($levels, 'value'),
            array_column($levels, 'name')
        );

        foreach ($bindUsers as $key=>$bindUser) {
            $userInfo = $userProfiles[$bindUser['bind_user_id']];
            $userInfo = array_merge($users[$bindUser['bind_user_id']], $userInfo);

            $userInfo['level_text'] = $levels[$userInfo['level']];

            $userProperty = $userPropertyList[$bindUser['bind_user_id']];

            $bindUsers[$key]['user_info'] = $userInfo;
            $bindUsers[$key]['user_property'] = $userProperty;
        }

        return $bindUsers;
    }

    public static function bind($userId, $bindUserId)
    {
        if (!self::isBind($userId, $bindUserId)) {
            return BindUser::insertGetId([
                'user_id' => $userId,
                'bind_user_id' => $bindUserId,
                'created_time' => date('Y-m-d H:i:s')
            ]);
        }
    }

    public static function unbind($userId, $bindUserId)
    {
        if (self::isBind($userId, $bindUserId)) {
            BindUser::where('user_id', $userId)->where('bind_user_id', $bindUserId)->delete();
        }
    }

    public static function isBind($userId, $bindUserId)
    {
        return BindUser::where('user_id', $userId)->where('bind_user_id', $bindUserId)->count() > 0;
    }

    public static function receive($userId, $userIds = null)
    {
        if (is_null($userIds)) {
            $userIds = BindUser::where('user_id', $userId)->pluck('bind_user_id')->toArray();
        } else {
            $userIds = BindUser::where('user_id', $userId)->whereIn('bind_user_id', $userIds)->pluck('bind_user_id')->toArray();
        }

        if (count($userIds)) {
            $userPropertyList = UserProperty::whereIn('user_id', $userIds)->get()->toArray();

            foreach ($userPropertyList as $userProperty) {
                if ($userProperty['integral'] > 0) {
                    PropertyService::add(get_user_id(), PayFactory::INTEGRAL, $userProperty['integral'], UserPropertyLog::INTEGRAL_FROM_OTHER_PERSON, '');
                    PropertyService::reduce($userProperty['user_id'], PayFactory::INTEGRAL, $userProperty['integral'], UserPropertyLog::INTEGRAL_TO_OTHER_PERSON, '');
                }

                if ($userProperty['register_integral'] > 0) {
                    PropertyService::add(get_user_id(), PayFactory::REGISTER_INTEGRAL, $userProperty['register_integral'], UserPropertyLog::REGISTER_INTEGRAL_FROM_OTHER_PERSON, '');
                    PropertyService::reduce($userProperty['user_id'], PayFactory::REGISTER_INTEGRAL, $userProperty['register_integral'], UserPropertyLog::REGISTER_INTEGRAL_TO_OTHER_PERSON, '');
                }

                if ($userProperty['recharge_integral'] > 0) {
                    PropertyService::add(get_user_id(), PayFactory::RECHARGE_INTEGRAL, $userProperty['recharge_integral'], UserPropertyLog::RECHARGE_INTEGRAL_FROM_OTHER_PERSON, '');
                    PropertyService::reduce($userProperty['user_id'], PayFactory::RECHARGE_INTEGRAL, $userProperty['recharge_integral'], UserPropertyLog::RECHARGE_INTEGRAL_TO_OTHER_PERSON, '');
                }


            }
        }
    }
}